<?php
declare(strict_types=1);

namespace WebApp\lib\db\redis;


use Throwable;

class RedisSwooleDriver extends RedisDriver
{
    public function __construct(RedisConfig $config)
    {
        parent::__construct($config);
    }

    /**
     * @param string $key
     * @return mixed
     */
    public function get(string $key)
    {
        return $this->getPool()->exec(function ($client) use ($key) {
            /** @var Redis $client */
            return $client->get($this->getDbName().':'.$this->getTableName().':'.$key);
        });
    }

    /**
     * @return mixed
     */
    public function hGetAll()
    {
        $all = $this->getPool()->exec(function ($client) {
            /** @var Redis $client */
            return $client->hGetAll($this->getDbName().':'.$this->getTableName());
        });
//        $res = [];
//
//        tracker(I, var_export($all, true));
//
//
//
//        foreach ($all as $index => $val) {
//            if ($index & 1 === 1) {
//                $res[$all[$index - 1]] = $val;
//            }
//        }
//        return $res;
        return $all;
    }

    /**
     * @return mixed
     */
    public function del()
    {
        return $this->getPool()->exec(function ($client) {
            /** @var Redis $client */
            return $client->del($this->getDbName().':'.$this->getTableName());
        });
    }

    /**
     * @param string $key
     * @param string $data
     */
    public function set(string $key, string $data)
    {
        $this->getPool()->exec(function ($client) use ($key, $data) {
            /** @var Redis $client */
            $client->set($this->getDbName().':'.$this->getTableName().':'.$key, $data);
        });
    }

    /**
     * @param string $key
     * @param string $data
     * @param int $time
     */
    public function setEx(string $key, string $data, int $time)
    {
        $this->getPool()->exec(function ($client) use ($key, $data, $time) {
            /** @var Redis $client */
            $client->setex($this->getDbName().':'.$this->getTableName().':'.$key, $time, $data);
        });
    }

    /**
     * @param string $key
     * @return mixed
     */
    public function hGet(string $key)
    {
        return $this->getPool()->exec(function ($client) use ($key) {
            /** @var Redis $client */
            return $client->hGet($this->getDbName().':'.$this->getTableName(), $key);
        });
    }

    /**
     * @param string $key
     * @param string $data
     */
    public function hSet(string $key, string $data)
    {
        $this->getPool()->exec(function ($client) use ($key, $data) {
            /** @var Redis $client */
            $client->hSet($this->getDbName().':'.$this->getTableName(), $key, $data);
        });
    }

    /**
     * @param string ...$key
     * @return mixed
     */
    public function hDel(string ...$key)
    {
        tracker(I, "hDel");
        return $this->getPool()->exec(function ($client) use ($key) {
            /** @var Redis $client */
            return $client->hDel($this->getDbName().':'.$this->getTableName(), ...$key);
        });
    }

    /**
     * @return RedisPool
     */
    public function getPool(): RedisPool
    {
        if (!$this->pool) {
            $this->pool = new RedisPool($this->config);
        }
        return  $this->pool;
    }

    public function getCon()
    {
        // TODO: Implement getCon() method.
    }

    /**
     * @param $conn
     * @param Throwable $e
     * @return mixed
     */
    public function catchErr(&$conn, Throwable $e)
    {
        // TODO: Implement catchErr() method.
    }

    public function getTryCount(): int
    {
        return 1;
    }
}
